package com.ftg.learn.amqp.topic;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author 布衣
 */
public class Ptopic {

    public static void main(String[] args) {
        //基础信息
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("10.2.40.82");
        factory.setPort(5672);
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setVirtualHost("/demo");

        //拿到连接
        try (
                Connection connection = factory.newConnection();
        ) {
            //创建channel
            Channel channel = connection.createChannel();

            String exName = "myTopic";
            String routingKey = "warning.abc.e";

            channel.exchangeDeclare(exName, BuiltinExchangeType.TOPIC);

            //发送信息
            String word = "系统消息： myTopic";
            //   发送
            channel.basicPublish(exName, routingKey, null, word.getBytes());
            System.out.println("发送成功");
        } catch (IOException | TimeoutException e) {
            throw new RuntimeException(e);
        }
    }

}
